package newflink3;

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.sink.KafkaRecordSerializationSchema;
import org.apache.flink.connector.kafka.sink.KafkaSink;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.time.Duration;

public class job2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);
        env.enableCheckpointing(5000);

        KafkaSource<String> source = KafkaSource.<String>builder()
                .setBootstrapServers("bigdata1:9092")
                .setGroupId("newflink3 job2")
                .setTopics("ods_mall_log")
                .setStartingOffsets(OffsetsInitializer.earliest())
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();

        WatermarkStrategy<String> watermarkStrategy = WatermarkStrategy.<String>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                .withTimestampAssigner(new SerializableTimestampAssigner<String>() {
                    @Override
                    public long extractTimestamp(String s, long l) {
                        return 0;
                    }
                });

        WatermarkStrategy<String> watermarks = WatermarkStrategy.<String>noWatermarks();

        KafkaSink<String> kafkaSink = KafkaSink.<String>builder()
                .setBootstrapServers("bigdata1:9092")
                .setRecordSerializer(KafkaRecordSerializationSchema.builder()
                .setTopic("dim_customer_login_log")
                .setValueSerializationSchema(new SimpleStringSchema())
                .build())
                .build();

        SingleOutputStreamOperator<String> filter = env.fromSource(source, watermarks, "kafka3job2")
                .filter(s -> s.contains("customer_login_log"));

        filter.sinkTo(kafkaSink);


        env.execute();
    }
}
